package ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.WorkListAdapter;
import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.ItemBean;
import bean.WorkListBean;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.log.LogUtil;
import view.BottomListPopWindow;
import view.WrapContentLinearLayoutManager;

/**
 * 作业列表 页面
 */
public class WorkActivity extends BaseNetActivity{
    private RadioGroup radioGroup;
    private RadioButton rb_all,
                        rb_nofinish,
                        rb_finish;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private WorkListAdapter adapter;
    private List<WorkListBean.mList> work_list = new ArrayList();
    private List<ItemBean> list = new ArrayList<>();
    private String subject_id,subject_name;
    private int type = 0; //0所有 1未提交 2已提交
    private String time_type = "start_time";
    private int page = 1;
    private View layoutview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setBaseRightIcon1(R.mipmap.shaixxuan, "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final BottomListPopWindow popWindow  =  new BottomListPopWindow(WorkActivity.this,list);
                popWindow.showAtBottom(layoutview);
                popWindow.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        toast(list.get(position).str);
                        work_list.clear();
                        page = 1;
                        if(position == 0){
                            time_type = "end_time";
                            loadData("0");
                        }else {
                            time_type = "start_time";
                           // start_time:发布时间排序 end_time:截止时间排序
                            loadData("0");
                        }
                        popWindow.dismiss();
                    }
                });
            }
        });

    }

    @Override
    public void success(String action, BaseBean baseBean) {
        swipeRefreshLayout.setRefreshing(false);
        adapter.loadMoreEnd();
        if(baseBean.code == 200){
            WorkListBean bean = (WorkListBean) GsonUtils.getInstance(this).parseComplexJArrayByCommon(baseBean,WorkListBean.class);
            work_list.addAll(bean.list) ;
            rb_nofinish.setText("未完成("+bean.unfinished+")");
            rb_finish.setText("已完成("+bean.finished+")");
            if(work_list == null){
                work_list = new ArrayList<>();
            }
            if(page>bean.page.total_page){
                LogUtil.app("已是最后一页");
            }else {
                page++;
                adapter.setNewData(work_list);
            }

        }else {
            toast(baseBean.msg);
        }
    }

    @Override
    public void error(String action, Throwable e) {
        swipeRefreshLayout.setRefreshing(false);
        adapter.loadMoreEnd();
    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.activity_work, null);
        return R.layout.activity_work;
    }

    @Override
    protected void findViews() {
        swipeRefreshLayout = getView(R.id.swipeRefreshLayout);
        radioGroup = getView(R.id.rd_group);
        rb_all = getView(R.id.rb_all);
        rb_nofinish = getView(R.id.rb_timetable);
        rb_finish = getView(R.id.rb_prepare);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        adapter = new WorkListAdapter(this,R.layout.item_work_list,work_list);
        recyclerView.setAdapter(adapter);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    //全部
                    case R.id.rb_all:
                        type = 0;
                        page = 1;
                        if(work_list!=null&&work_list.size()>0){
                            work_list.clear();
                            adapter.notifyDataSetChanged();
                        }
                        loadData("0");
                       // toast("全部");
                        break;
                    //未完成
                    case R.id.rb_timetable:
                        type = 1;
                        page = 1;
                        if(work_list!=null&&work_list.size()>0){
                            work_list.clear();
                            adapter.notifyDataSetChanged();
                        }

                        loadData("0");
                       // toast("未完成");
                        break;
                    //已完成
                    case R.id.rb_prepare:
                        type = 2;
                        page = 1;
                        if(work_list!=null&&work_list.size()>0){
                            work_list.clear();
                            adapter.notifyDataSetChanged();
                        }
                        loadData("0");
                       // toast("已完成");
                        break;
                }
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.bt_answer:
                        Button button = (Button) view;
                        if(work_list.size()<=0){return;}
                        if(button.getText().toString().equals("答题")){
                            Bundle bundle = new Bundle();

                            bundle.putInt("paper_id",work_list.get(position).paper_id);
                            bundle.putInt("student_task_id",work_list.get(position).student_task_id);
                            bundle.putString("title",subject_name+"作业");
                            if(work_list.get(position).paper_type == 3){
                                jumpTo(DoTheImageWorkActivity.class,bundle);
                            }else {
                                jumpTo(DoTheWorkActivity.class,bundle);
                            }
                        }else {
                            Bundle bundle = new Bundle();
                            bundle.putInt("answer_id",work_list.get(position).answer_id);
                            bundle.putInt("student_task_id",work_list.get(position).student_task_id);
                            jumpTo(AnswerSheetActivity.class,bundle);
                        }

                        break;
                }
            }
        });
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEnableLoadMore(true);
        adapter.openLoadAnimation();
        adapter.disableLoadMoreIfNotFullPage();
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                work_list.clear();
                adapter.notifyDataSetChanged();
                page = 1;
                loadData("0");
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadData("0");
                    }
                },500);

            }
        });
    }

    @Override
    protected void formatData() {
//        for(int i=0;i<3;i++){
//            PrePareOrReViewDetailBean.taskData workBean = new PrePareOrReViewDetailBean.taskData();
//            work_list.add(workBean);
//        }
//        work_list.clear();
//        adapter.notifyDataSetChanged();
//        page = 1;
//        loadData("0");
        ItemBean bean = new ItemBean();
        bean.setStr("按截止时间排序");
        ItemBean bean1 = new ItemBean();
        bean1.setStr("按布置时间排序");
        list.add(bean);
        list.add(bean1);
    }
    private void loadData(String sort_type){
      Map<String,String> map = new HashMap<>();
      map.put("subject_id",""+subject_id);
      map.put("type",type+"");
      map.put("sort",time_type);// start_time:发布时间排序 end_time:截止时间排序
      map.put("sort_type",sort_type);    //   0:降序（默认） 1:升序
      map.put("page",page+"");//  页数，默认为1
      map.put("page_number","8");
      post("Task/taskList",map,BaseBean.class,false);
    }
    @Override
    protected void getBundle(Bundle bundle) {
        subject_id  = bundle.getString("subject_id");
        subject_name = bundle.getString("subject_name");
        setTitle(bundle.getString("subject_name")+"作业");
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        work_list.clear();
        adapter.notifyDataSetChanged();
        page = 1;
        loadData("0");
    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
